package ru.ifmo.kis;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

/**
 * This GUI is created with help of IntelliJ IDEA.
 */
public class ImitateGUI {
	private JButton generateButton;
	private JButton exitButton;
	private JComboBox comboBox1;
	private JPanel mainPanel;
	private JSpinner spinnerRandom;
	private JSpinner spinner3;
	private JSpinner spinnerMaxOrders;
	private JTextField textFieldTopIntValue;
	private JButton theButtonButton;
	private JTextPane textPane1;
	private JTabbedPane tabs;
	private JTextField textField2;
	private JPanel bottomPanel;
	private ImitateGUI.GraphicPainter painter;
	private JTextField a10TextField;
	private JTextField a5TextField;

	private BufferedImage plot;

	public ImitateGUI() {
		// defaults:
		spinnerMaxOrders.setValue(10);


		exitButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		generateButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int randomSeed = (Integer) spinnerRandom.getValue();
				int maxOrders = (Integer) spinnerMaxOrders.getValue();

				ImitatingModeling modeling = new ImitatingModeling(randomSeed, maxOrders);
				textPane1.setText(modeling.randomOrders());
				tabs.setSelectedIndex(1);
			}
		});

		theButtonButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				double topIntValue;
				try {
					topIntValue = Double.parseDouble((String) textFieldTopIntValue.getText());
				} catch (NumberFormatException nfe) {
					topIntValue = 100.;
					JOptionPane.showMessageDialog(mainPanel, "Bad topIntValue. Use " + topIntValue);
				}

				double devider;
				try {
					devider = Double.parseDouble((String) textField2.getText());
				} catch (NumberFormatException nfe) {
					devider = 6.;
					JOptionPane.showMessageDialog(mainPanel, "Bad devider. Use " + devider);
				}

				double param1;
				param1 = Double.parseDouble((String) a10TextField.getText());

				double param2;
				param2 = Double.parseDouble((String) a5TextField.getText());

				ImitatingModeling.randomDistributionChart(plot, devider, (int) topIntValue, param1, param2);
				painter.repaint();
			}
		});
	}

	private void createUIComponents() {
		plot = new BufferedImage(300, 200, BufferedImage.TYPE_INT_ARGB);
		painter = new GraphicPainter(plot);
	}

	public static void main(String[] args) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (Exception ignore) {
		}


		JFrame frame = new JFrame("Imitate");
		frame.setContentPane(new ImitateGUI().mainPanel);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	/**
	 * Draw image.
	 */
	public class GraphicPainter extends JComponent {
		private final Dimension size = new Dimension(320, 240);
		private final Image plot;

		public GraphicPainter(Image plot) {
			this.plot = plot;
		}

		@Override
		public void paint(Graphics g) {
			Graphics2D g2 = (Graphics2D) g;
			Dimension size = getSize();
			Composite origComposite;

			origComposite = g2.getComposite();

			g2.setColor(Color.white);
			g2.fillRect(0, 0, getWidth(), getHeight());

			g2.drawImage(plot, 10, 0, null);
		}

		@Override
		public Dimension getPreferredSize(){
			return size;
		}

	}
}
